######################################################
# Code to create Figure OB1: Over Time Trends in Extremity Perceptions
######################################################

############################
#Packages
############################
library(survey)
library(srvyr)
library(directlabels)
library(rio)
library(directlabels)
library(tidyverse)

############################
#Import Data and Clean
############################

#######Data
combdata <- import("./Data/pew_cnn_comb.Rda")
combdata$sdate <- as.Date(combdata$sdate)       

#######Declare Survey Design
comb_design <- combdata %>%
  filter(!is.na(weight)) %>%  #This removes some observations with missing weight data
  srvyr::as_survey_design(ids = 1, weights=weight)



########Get weighted means
rep_means <- comb_design %>%
  mutate(rep_extreme = factor(rep_extreme, 
                              levels=c(0,1), 
                              labels=c("Not Too Extreme", "Too Extreme"))) %>%
  filter(!is.na(rep_extreme)) %>%
  group_by(sdate, rep_extreme) %>%
  summarize(proportion = srvyr::survey_mean(na.rm=T)) %>%
  mutate(lower = proportion - (1.96*proportion_se), 
         upper = proportion + (1.96*proportion_se), 
         target = "Republican Party") 


dem_means <- comb_design %>%
  mutate(dem_extreme = factor(dem_extreme, 
                              levels=c(0,1), 
                              labels=c("Not Too Extreme", "Too Extreme"))) %>%
  filter(!is.na(dem_extreme)) %>%
  group_by(sdate, dem_extreme) %>%
  summarize(proportion = srvyr::survey_mean(na.rm=T)) %>%
  mutate(lower = proportion - (1.96*proportion_se), 
         upper = proportion + (1.96*proportion_se), 
         target = "Democratic Party") 

comb_means <- bind_rows(rep_means, dem_means) %>%
  mutate(response = case_when(
    !is.na(rep_extreme) ~ rep_extreme, 
    is.na(rep_extreme) ~ dem_extreme)) %>%
  select(-rep_extreme, -dem_extreme)
  

#######Export Overall Means
export(comb_means, "./Data/party_means.csv")

#######Figure
c1 <- comb_means %>% filter(response == "Too Extreme")

ends <- c1 %>%
  group_by(target) %>%
  top_n(1, sdate) %>%
  pull(proportion)

trends_fig <- ggplot(c1, aes(x=sdate, y=proportion, shape=target, linetype=target)) + 
  geom_point() + 
  geom_line() + 
  scale_y_continuous(limits=c(0,1), 
                     sec.axis=sec_axis(~., breaks=ends, 
                                       label=c("Republican", "Democratic"))) + 
  theme_bw() + 
  labs(title="Party Too Extreme?", 
       y = "% Saying Too Extreme", 
       x = "Year of Survey", 
       shape="Party", 
       linetype="Party") + 
  scale_x_date(date_breaks= "1 year", date_labels="%Y", 
               expand = c(0, 0)) + 
  theme(legend.position=c(0.30,0.95),
        legend.direction="horizontal", 
        legend.background = element_rect(), 
        legend.box.background = element_rect(colour = "black")) 

trends_fig1 <- lemon::reposition_legend(trends_fig, 'top left', offset=0.002)

ggsave(plot = trends_fig1, "figure_ob1.png", 
       height=6, width=10, dpi=1200)
